#pragma once
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include "log.hpp"
void serviceIO(int sock)
{
    while (true)
    {

        if (sock < 0)
        {
            std::cout << "accept error, next" << std::endl;
            continue;
        }

        // 接收消息
        char buffer[1024];
        ssize_t len = recv(sock, buffer, sizeof(buffer), 0);
        buffer[len] = 0;
        std::cout << "成功收到客户端消息，消息为：" << buffer << std::endl;

        // 应答消息
        std::string ack = "服务端回显";
        ssize_t n = send(sock, ack.c_str(), ack.size(), 0);

        if (n > 0)
        {
            logMessage(NORMAL, "send success:size:%d,fd:%d", n, sock);
        }
        else 
        {
            logMessage(ERROR, "send successfd:%d", sock);
        }
    }
    close(sock);
}